<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Model_Post extends Zend_Db_Table{
	
	protected $_name    = "posts";
    protected $_primary = "id";
    
    public function saveData($data = array()){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            if(isset($data['id']) && $this->checkExists($data['id'])){
                $id = $data['id'];
                $db->update("posts", $data, "id = $id");
            }else{
                $db->insert('posts', $data);
            }
            
            $cache = new Zenddn_System_Cache();
            $cache->clean(array('post'));
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getData($id = null){
    	$cache = new Zenddn_System_Cache();
        $cache = $cache->getCache();        
        $cache_id = "post_getData_{$id}";
        
        if(($result = $cache->load($cache_id)) === false){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_OBJ);
            
            $cols = array('id', 'title', 'description', 'content', 'image', 'publish_date', 'status');
            
            if($id != null){
                $cols = array('id', 'title', 'description', 'content', 'category', 'image', 'publish_date', 'status');
            }
            
            $select = $db->select()
                        ->from(array('p' => 'posts'), $cols);
            
            if($id != null){
                $select->where("p.id = ?", $id, 'INTEGER');
                
                $db->setFetchMode(Zend_Db::FETCH_ASSOC);
                $result = $db->fetchRow($select);
            }else{
                $select->join(array('t' => 'taxonomy'), 't.id = p.category', 'name AS category')
                        ->order('p.id DESC');
                
                $result = $db->fetchAll($select);
            }
            
            $cache->save($result, $cache_id, array('post'));
        }
        
        return $result;
    }
    
    public function getDataByCategory($id, $count = 1){
        $cache = new Zenddn_System_Cache();
        $cache = $cache->getCache();        
        $cache_id = "post_getDataByCategory_{$id}_{$count}";
        
        if(($result = $cache->load($cache_id)) === false){
        	$db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_OBJ);
            
            $cols = array('id', 'title', 'description', 'content', 'category', 'image', 'publish_date', 'status');
            
            $select = $db->select()
                        ->from(array('p' => 'posts'), $cols)
                        ->where("p.category = ?", $id, 'INTEGER')
                        ->where('p.status = 1')
                        ->order('p.id DESC');
            
            if($count != 0){
                $select->limit($count);
                
                $db->setFetchMode(Zend_Db::FETCH_ASSOC);
                
                $result = $db->fetchRow($select);
            }else{
                $result = $db->fetchAll($select);
            }
            
            $cache->save($result, $cache_id, array('post'));
        }
        
        return $result;
    }
    
    public function getAllData(){
    	return $this->getData();
    }
    
    public function deleteData($id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->delete('posts', "id = $id");
            
            $cache = new Zenddn_System_Cache();
            $cache->clean(array('post'));
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function checkExists($id){
        $option = array('table' => 'posts', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
}